package com.htht.job.admin.controller.appinterface;

import com.alibaba.fastjson.JSON;
import com.htht.job.admin.core.enums.ExecutorFailStrategyEnum;
import com.htht.job.admin.core.model.XxlJobGroup;
import com.htht.job.admin.core.route.ExecutorRouteStrategyEnum;
import com.htht.job.admin.dao.XxlJobGroupDao;
import com.htht.job.admin.service.XxlJobService;
import com.htht.job.core.api.DubboService;
import com.htht.job.core.api.parameter.JobParameterService;
import com.htht.job.core.api.parameter.ParameterService;
import com.htht.job.core.enums.ExecutorBlockStrategyEnum;
import com.htht.job.core.glue.GlueTypeEnum;
import com.htht.job.core.util.ResultUtil;
import com.htht.job.core.util.ReturnCodeEnum;
import com.htht.job.executor.model.parameter.ParameterModel;
import com.htht.job.executor.model.product.Product;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzj on 2018/3/14.
 */
@RestController
@RequestMapping("/api/jobinfo")
@Api(value = "/api/jobinfo",description="任务列表相关接口")
public class AppJobInfoController {

    @Resource
    private XxlJobGroupDao xxlJobGroupDao;
    @Resource
    private XxlJobService xxlJobService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private DubboService dubboService;
    @Resource
    private JobParameterService jobParameterService;
    @RequestMapping(value="/{taskType}",method = RequestMethod.POST,produces = { "application/json;charset=UTF-8" })
    @ApiOperation(value = "根据任务类型获取添加任务下拉框值", notes = "获取添加任务下拉框值", httpMethod = "POST")
    public ResultUtil<Map> index(@ApiParam(name="jobGroup",required = false,value="任务组") @RequestParam(required = false, defaultValue = "-1") int jobGroup,
                                 @ApiParam(name="taskType",required = true,value = "任务类型") @PathVariable String taskType) {
        ResultUtil<Map> result=new ResultUtil<Map>();
        Map map=new HashMap();

        // 枚举-字典
        map.put("ExecutorRouteStrategyEnum", ExecutorRouteStrategyEnum.values());	// 路由策略-列表
        map.put("GlueTypeEnum", GlueTypeEnum.values());								// Glue类型-字典
        map.put("ExecutorBlockStrategyEnum", ExecutorBlockStrategyEnum.values());	// 阻塞处理策略-字典
        map.put("ExecutorFailStrategyEnum", ExecutorFailStrategyEnum.values());		// 失败处理策略-字典

        /**============获取模型下拉框============**/
        List<ParameterModel> executorHandlerlist=parameterService.findListParameter();
        List<Product> productList=dubboService.findALlProduct();
        map.put("executorHandlerlist",executorHandlerlist);
        map.put("productList",productList);

        // 任务组
        List<XxlJobGroup> jobGroupList =  xxlJobGroupDao.findAll();
        map.put("JobGroupList", jobGroupList);
        map.put("jobGroup", jobGroup);
        map.put("taskType", taskType);
        result.setResult(map);
        return result;
    }

    @RequestMapping(value="/pageList/{taskType}",method = RequestMethod.POST,produces = { "application/json;charset=UTF-8" })
    @ApiOperation(value = "根据任务类型查询任务列表", notes = "任务列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name="start",required = false,value="起始页",defaultValue="0"),
            @ApiImplicitParam(paramType="query", name="length",required = false,value="条数",defaultValue="10"),
            @ApiImplicitParam(paramType="query", name="jobGroup",required = false,value="任务组" ),
            @ApiImplicitParam(paramType="query", name="executorHandler",required = false,value="执行器"),
            @ApiImplicitParam(paramType="query", name="filterTime",required = false,value="时间"),
            @ApiImplicitParam(paramType="path", name="taskType",required = true,value="任务类型")

    })
    public ResultUtil<Map> pageList(@RequestParam(required = false, defaultValue = "0") int start,
                                    @RequestParam(required = false, defaultValue = "10") int length,
                                    @RequestParam(required = true, defaultValue = "-1") int jobGroup,
                                    @RequestParam(required = false, defaultValue = "")  String executorHandler,
                                    @RequestParam(required = false, defaultValue = "") String filterTime,
                                    @PathVariable("taskType") String taskType) {
        ResultUtil<Map> result=new ResultUtil<Map>();
        if(StringUtils.isEmpty(taskType)){
            result.setErrorMessage(ReturnCodeEnum.ReturnCodeEnum_201_ERROR);
        }
        if(!result.isSuccess()){
            return result;
        }

        Map<String,Object> map=xxlJobService.pageList(start, length, jobGroup, executorHandler, filterTime,taskType);
        result.setResult(map);
        return result;
    }
}
